<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="司机姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="驾驶证" prop="license">
      <el-input v-model="dataForm.license" placeholder="驾驶证"></el-input>
    </el-form-item>
    <el-form-item label="驾龄" prop="years">
      <el-input-number style="width: 100%"  v-model="dataForm.years" type="number" @input="limitPhoneNum"  placeholder="驾龄"></el-input-number >
    </el-form-item>
    <el-form-item label="联系地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="联系地址"></el-input>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {isMobile } from '@/utils/validate'
  import Utils from '@/utils/utils'

  export default {
    data () {
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          phone: '',
          license: '',
          years: '',
          address: ''
        },
        dataRule: {
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ],
          license: [
            { required: true, message: '驾驶证不能为空', trigger: 'blur' }
          ],
          years: [
            { required: true, message: '驾龄不能为空', trigger: 'blur' }
          ],
          address: [
            { required: false, message: '联系地址不能为空', trigger: 'blur' }
          ]

        }
      }
    },
    methods: {
      limitPhoneNum(value) {
        if (value.toString().length > 2) {
          this.dataForm.years = this.dataForm.years.toString().slice(0, 2)
        }
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tms/driver/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.driver.name
                this.dataForm.phone = data.driver.phone
                this.dataForm.license = data.driver.license
                this.dataForm.years = data.driver.years
                this.dataForm.address = data.driver.address
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit: Utils.debounce(function () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tms/driver/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'phone': this.dataForm.phone,
                'license': this.dataForm.license,
                'years': this.dataForm.years,
                'address': this.dataForm.address
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }, 1000)
    }
  }
</script>
